<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-12-16
 * Time: 10:08
 */

namespace app\water\controller;
/** 控制器类引入区 **/

use app\common\controller\Smscm;

/** 模型类引入区 **/

use app\common\model\Users;
use app\common\model\Users as UserM;
use think\Db;
use think\Validate;

/**
 * 前台首页控制器
 * @package app\api\controller
 */
class Config extends Auth
{
    public $noNeedLogin = [];

    /**
     * 获取水吧配置信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $result = Db::name('qt_water_config')->where('store_id', $this->userInfo['store_id'])->find();

            //不接受的商品分类
            $noreceive_product = explode(',', $result['noreceive_product']);
            $noreceive = Db::name('qt_goods_category')->whereIn('id', $noreceive_product)->column('name');
            $result['noreceive_name'] = implode(',', $noreceive);

            //不打印的商品
            $noprint_product = explode(',', $result['noprint_product']);
            $noprint = Db::name('qt_goods_category')->whereIn('id', $noprint_product)->column('name');
            $result['noprint_name'] = implode(',', $noprint);

            //不接收的房间
            //$noreceive_room = explode(',',$result['noreceive_room']);
            //$name = Db::name('qt_room')->whereIn('id',$noprint_product)->column('name');
            //$result['noreceive_room'] = implode(',',$name);
            $this->result('查询成功', $result, 200);
        }
        $this->result('无效的请求方式', '', 204);
    }


    /**
     * 保存水吧配置信息
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $rule = [
                'id|ID' => 'require|number',
                'hidden|隐藏确认信息' => 'require|number',
                'auto_print|自动打印茶水信息' => 'require|number',
                'call_print|呼叫服务打印' => 'require|number',
                'print_confirm|打印并确认' => 'require|number',
                'show_time|展示时间' => 'require|number',
                'print_machine|打印机器' => 'require',
                'print_number|打印份数' => 'require|number',
                'play_speed|播放速度' => 'require',
            ];
            // 定义验证提示
            $message = [
                'id.require' => 'ID不能为空',
                'id.number' => 'ID必须为数字',
                'hidden.require' => '请输入隐藏确认信息',
                'hidden.number' => '隐藏确认信息格式不正确',
                'auto_print.require' => '自动打印茶水信息不能为空',
                'auto_print.number' => '自动打印茶水信息必须为数字',
                'call_print.require' => '呼叫服务打印不能为空',
                'call_print.number' => '呼叫服务打印必须为数字',
                'print_confirm.require' => '打印并确认不能为空',
                'print_confirm.number' => '打印并确认必须为数字',
                'show_time.require' => '展示时间不能为空',
                'show_time.number' => '展示时间必须为数字',
                'print_machine.require' => '打印机器不能为空',
                'print_number.require' => '打印份数不能为空',
                'print_number.number' => '打印份数必须为数字',
                'play_speed.require' => '播放速度不能为空',
                'play_speed.number' => '播放速度必须为数字',
            ];
            $validate = new Validate($rule, $message);
            if (!$validate->check($data)) $this->result($validate->getError(), '', 204);
            Db::name('qt_water_config')->where('id', $data['id'])->update($data);
            $this->result('保存成功', '', 200);
        }else {
            $this->result('无效的请求方式', '', 204);
        }
    }


    /**
     * 获取房间列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function room_list()
    {
        if ($this->request->isPost()) {
            $data = Db::name('qt_room')
                ->field('id,name')
                ->where('store_id', $this->userInfo['store_id'])
                ->select();
            $this->result('查询成功', $data, 200);
        }
    }


    /**
     * 获取商品分类列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function product_type()
    {
        if ($this->request->isPost()) {
            $data = Db::name('qt_goods_category')
                ->field('id,name')
                ->where('store_id', $this->userInfo['store_id'])
                ->where('status',1)
                ->select();
            $this->result('查询成功', $data, 200);
        }
    }

}
